<?php
class plugins
{
	private $db;
	public $page;
	
	function __construct()
	{
		global $db;
		$this->db = $db;
	}
	
	/**
	 * 查找插件方法详细列表
	 *
	 * @param array $condition
	 * @param int $page
	 * @param int $pagesize
	 * @return array
	 */
	function listinfo($condition,$page,$pagesize)
	{
		$where = ' where status>?';
		$params = array();
		if(isset($condition['plugins']) && !empty($condition['plugins']))
		{
			$params[] = array('string', $condition['plugins']);
			$where .= ' and plugins=?';
		}
		if(isset($condition['hook']) && !empty($condition['hook']))
		{
			$params[] = array('string', $condition['hook']);
			$where .= ' and hook=?';
		}
		if(isset($condition['siteid']) && !empty($condition['siteid']))
		{
			$params[] = array('string', $condition['siteid']);
			$where .= ' and (siteid=? or siteid="")';
		}
		$this->db->prepare('select count(*) as nums from '.DB_PRE.'plugins_hooks '.$where);
		$this->db->set_int(0);
		if(is_array($params))
		{
			foreach ($params as $p)
			{
				$func = 'set_' . $p[0];
				$this->db->$func($p[1]);
			}
		}
		$res = $this->db->execute(true);
		$size = $res['nums'];
		$start = ($page - 1) * $pagesize;
		$this->db->prepare('select * from '.DB_PRE.'plugins_hooks '.$where.' limit ?,?');
		$this->db->set_int(0);
		if(is_array($params))
		{
			foreach ($params as $p)
			{
				$func = 'set_' . $p[0];
				$this->db->$func($p[1]);
			}
		}
		$this->db->set_int($start);
		$this->db->set_int($pagesize);
		$res = $this->db->execute();
		$this->pages = pages($size, $page, $pagesize);
		return $res;
	}
	
	/**
	 * 更改记录状态
	 *
	 * @param int $id
	 * @return bool
	 */
	function change_status($id)
	{
		$this->db->prepare('select status from '.DB_PRE.'plugins_hooks where id=?');
		$this->db->set_int($id);
		$res = $this->db->execute(true);
		if(is_array($res) && array_key_exists('status',$res))
		{
			$status = $res['status']==99 ? 1 : 99;
			$this->db->prepare('update '.DB_PRE.'plugins_hooks set status=? where id=?');
			$this->db->set_int($status);
			$this->db->set_int($id);
			return $this->db->execute();
		}
		else 
		{
			return true;
		}
	}
	
	/**
	 * 删除关联
	 *
	 * @param int $id
	 * @return bool
	 */
	function delete($id)
	{
		$this->db->prepare('delete from ' . DB_PRE .'plugins_hooks where id=?');
		$this->db->set_int($id);
		$res = $this->db->execute();
		return $res;
	}
	
	
	/**
	 * 获得已存在的插件
	 *
	 */
	function get_exists_plugins()
	{
		//获得核心框架中的插件
		$plugins_file = array();
		if (is_dir(APP_ROOT)) 
		{
		    if ($dh = opendir(APP_ROOT.'plugins')) 
		    {
		        while (($file = readdir($dh)) !== false) 
		        {
		            if(substr($file,-12) == '.plugins.php')
		            {
		            	$plugins_file[] = 'plugins/'.$file;
		            }
		        }
		        closedir($dh);
		    }
		    $modules = get_moudule_info();
		    if(is_array($modules))
		    {
		    	foreach ($modules as $mod)
		    	{
		    		$dir = APP_ROOT.$mod['name'].'/plugins';
		    		if (is_dir($dir)) 
					{
					    if ($dh = opendir($dir)) 
					    {
					        while (($file = readdir($dh)) !== false) 
					        {
					            if(substr($file,-12) == '.plugins.php')
					            {
					            	$plugins_file[] = $mod['name'].'/plugins/'.$file;
					            }
					        }
					        closedir($dh);
					    }
					}
		    	}
		    }
		    
		}
		$plugins = array();
		if(is_array($plugins_file))
		{
			foreach ($plugins_file as $p)
			{
				include_once(APP_ROOT . $p);
				$class = 'plugins_'.substr(basename($p),0,-12);
				$tmp_obj = new $class;
				$tmp_obj->install();
				$plugins[$p] = array($p,substr(basename($p),0,-12),$tmp_obj->info['classname']);
			}
		}
		return $plugins;
	}
	
	/**
	 * 获取可用的插件列表
	 *
	 * @return array
	 */
	function get_plugins()
	{
		$plugins = $this->get_exists_plugins();
		return $plugins;
	}
	
	/**
	 * 检查插件是否已经被安装
	 *
	 * @param  string $plugins
	 * @return bool
	 */
	function check_plugins_installed($plugins)
	{
		$this->db->prepare('select count(*) as nums from ' . DB_PRE .'plugins_hooks where plugins=?');
		$this->db->set_string($plugins);
		$res = $this->db->execute(true);
		if(isset($res['nums']) && intval($res['nums']) > 0)
		{
			return true;
		}
		return false;
	}
	
	/**
	 * 添加记录
	 *
	 * @param array $params
	 */
	function add_func_hook($params)
	{
		$res = $this->db->insert(DB_PRE .'plugins_hooks', $params);
		return $res;
	}
	
	/**
	 * 检查插件的方法是否被安装
	 *
	 * @param string $plugins
	 * @param string $func
	 * @return bool
	 */
	function check_func_hook_add($plugins,$func,$siteid)
	{
		$this->db->prepare('select count(*) as nums from ' . DB_PRE .'plugins_hooks where plugins=? and func=? and (siteid="" or siteid=?)');
		$this->db->set_string($plugins);
		$this->db->set_string($func);
		$this->db->set_string($siteid);
		$res = $this->db->execute(true);
		if(isset($res['nums']) && intval($res['nums']) > 0)
		{
			return true;
		}
		return false;
	}
	
	/**
	 * 获得插件注册的方法列表
	 *
	 * @param string $plugins
	 * @return array
	 */
	function get_plugin_funcs($plugins)
	{
		if(!is_file(APP_ROOT.$plugins) || !file_exists(APP_ROOT.$plugins))
		{
			return false;
		}
		include_once(APP_ROOT.$plugins);
		$class = 'plugins_'.substr(basename($plugins),0,-12);
		$tmp_obj = new $class;
		$tmp_obj->install();
		$list = array();
		if(is_array($tmp_obj->info['funcs']))
		{
			foreach ($tmp_obj->info['funcs'] as $func=>$info)
			{
				$list[$func] = $info['name'];
			}
		}
		return $list;
	}
	
	
}
?>